import 'package:get/get.dart';
import 'package:kkhousekeep/models/order_list_entity.dart';
import 'package:kkhousekeep/models/property_order_detail_entity.dart';
import 'package:kkhousekeep/net/home_order_connect.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../../../../config/constant.dart';
import '../../../public/take_picture_screen.dart';

class OrderTakingController extends GetxController {
  late OrderListObj _orderEntity;
  var orderId = 0.obs;
  var fatalType = "".obs;
  var address = "".obs;
  var fatalDesc = "".obs;
  var descImg = <String>[].obs;
  var woSn = "".obs;
  late HomeOrderConnect _homeOrderConnect;

  @override
  void onInit() {
    _homeOrderConnect = Get.find<HomeOrderConnect>();
    _orderEntity = Get.arguments;
    orderId.value = _orderEntity.id;
    fatalType.value = _orderEntity.fatalType;
    address.value = _orderEntity.address;
    woSn.value = _orderEntity.woSn;
    super.onInit();
    _loadNetworkData();
  }

  Future<void> _loadNetworkData() async {
    PropertyOrderDetailEntity orderDetailEntity =
        await _homeOrderConnect.getOrderDetail(orderId: orderId.value);
    PropertyOrderDetailObj orderDetailObj = orderDetailEntity.obj;
    fatalType.value = orderDetailObj.fatalType;
    address.value = orderDetailObj.address;
    fatalDesc.value = orderDetailObj.fatalDesc;
    descImg.value = orderDetailObj.pics;
  }

  takeOrder() async {
    var response = await _homeOrderConnect.orderReceive(orderId: orderId.value);
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      Get.back(result: Constant.resultOK);
    } else {
      DialogUtil.showNetworkFail();
    }
  }

  void clickImageItem(String path) {
    Get.to(DisplayPictureScreen(
      imagePath: path,
      isShowRetake: false,
    ));
  }
}
